package com.pmpd.activity.service.api.impl;

import com.dmap.base.units.DateUtil;
import com.pmpd.activity.constants.ActivityConstant;
import com.pmpd.activity.dao.ActivityUserMapper;
import com.pmpd.activity.po.ActivityUser;
import com.pmpd.activity.po.ActivityUserExample;
import com.pmpd.activity.service.api.ApiActivityUserService;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author liuying
 * @Description 活动用户关联服务层实现
 * @Date 14:54 2018/6/7
 */
@Service
public class ApiActivityUserServiceImpl implements ApiActivityUserService {

    private static final Logger logger = LoggerFactory.getLogger(ApiActivityUserServiceImpl.class);

    @Resource
    private ActivityUserMapper activityUserMapper;

    @Override
    public int countByActivityAndType(long activityId, int type) {
        ActivityUserExample example = new ActivityUserExample();
        ActivityUserExample.Criteria criteria = example.createCriteria();
        criteria.andActivityIdEqualTo(activityId);
        criteria.andTypeEqualTo(type);

        return activityUserMapper.countByExample(example);
    }

    @Override
    public ActivityUser addActivity(long activityId, long userId, int type, String projectId, long seconds) {
        //当天零点时间戳
        long zero = DateUtil.getDay0ByTime(seconds);
        //当前时间的小时数
        int hours = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);

        ActivityUser activityUser = new ActivityUser();
        activityUser.setActivityId(activityId);
        activityUser.setUserId(userId);
        activityUser.setType(type);

        if (hours <= ActivityConstant.TASK_JUDGE_HOUR_OF_DAY){
            activityUser.setJoinTime(seconds);
            activityUser.setEndTime(DateUtil.getDay0ByTime(seconds) + (3600 * 24 * ActivityConstant.USER_PARTICIPATION_DAYS));
        } else {
            activityUser.setJoinTime(zero + (3600 * 24 * 1));
            activityUser.setEndTime(DateUtil.getDay0ByTime(seconds) + (3600 * 24 * (ActivityConstant.USER_PARTICIPATION_DAYS + 1)));
        }

        activityUser.setActivityStartTime(ActivityConstant.TIME_ACTIVITY_BEGIN);
        activityUser.setActivityEndTime(ActivityConstant.TIME_ACTIVITY_END);

        activityUser.setCreateTime(seconds);
        activityUser.setUpdateTime(activityUser.getCreateTime());
        activityUser.setProjectId(projectId);

        int success = activityUserMapper.insertSelective(activityUser);
        return (success == 1) ? activityUser : null;
    }

    @Override
    public ActivityUser ActivityUserByUserId(long activityId, long userId) {
        ActivityUserExample example = new ActivityUserExample();
        ActivityUserExample.Criteria criteria = example.createCriteria();
        criteria.andActivityIdEqualTo(activityId);
        criteria.andUserIdEqualTo(userId);

        List<ActivityUser> list = activityUserMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        } else {
            return null;
        }
    }

    @Override
    public int isWinning(long activityId, long userId) {
        ActivityUserExample example = new ActivityUserExample();
        ActivityUserExample.Criteria criteria = example.createCriteria();
        criteria.andActivityIdEqualTo(activityId);
        criteria.andUserIdEqualTo(userId);
        List<ActivityUser> users = activityUserMapper.selectByExample(example);

        return users.get(0).getWinningState();
    }

    @Override
    public List<Long> getAwardUser(long activityId, int awardDay) {
        ActivityUserExample example = new ActivityUserExample();
        ActivityUserExample.Criteria criteria = example.createCriteria();
        criteria.andActivityIdEqualTo(activityId);
        criteria.andAwardDayEqualTo(awardDay);
        criteria.andWinningStateEqualTo(ActivityConstant.STATE_WINNING);

        List<Long> resultList = new ArrayList<>();
        activityUserMapper.selectByExample(example).forEach(ActivityUser ->
                resultList.add(ActivityUser.getUserId())
        );

        return resultList;
    }

    @Override
    public ActivityUser updateFinishDays(long userId, long activityId, int finishDays) {
        ActivityUser user = new ActivityUser();
        user.setFinishDays(finishDays);
        user.setUpdateTime(System.currentTimeMillis() / 1000);

        ActivityUserExample example = new ActivityUserExample();
        example.createCriteria().andUserIdEqualTo(userId).andActivityIdEqualTo(activityId);

        int success = activityUserMapper.updateByExampleSelective(user, example);
        return (success == 1) ? user : null;
    }

    @Override
    public List<ActivityUser> selectByActivityId(long activityId) {
        ActivityUserExample example = new ActivityUserExample();
        ActivityUserExample.Criteria criteria = example.createCriteria();
        criteria.andActivityIdEqualTo(activityId);

        return activityUserMapper.selectByExample(example);
    }

    @Override
    public boolean bulkUpdate(List<ActivityUser> users) {
        long seconds = System.currentTimeMillis() / 1000;

        for (ActivityUser user : users){
            user.setUpdateTime(seconds);
        }
        logger.info("---------------同步用户任务完成天数数据开始------------");
        int success = activityUserMapper.bulkUpdate(users);
        if (success > 0) {
            logger.info("---------------同步用户任务完成天数数据成功------------");
            return true;
        } else {
            logger.info("---------------同步用户任务完成天数数据失败------------");
            return false;
        }
    }

    @Override
    public List<ActivityUser> selectByActivityIdAndUserId(long activityId, long userId) {
        ActivityUserExample example = new ActivityUserExample();
        example.createCriteria().andActivityIdEqualTo(activityId).andUserIdEqualTo(userId);

        return activityUserMapper.selectByExample(example);
    }

    @Override
    public ActivityUser updateWinningState(long activityId, long userId, int awardDay) {
        ActivityUserExample example = new ActivityUserExample();
        ActivityUserExample.Criteria criteria = example.createCriteria();
        criteria.andActivityIdEqualTo(activityId).andUserIdEqualTo(userId);

        ActivityUser user = new ActivityUser();
        user.setWinningState(ActivityConstant.STATE_JOIN_WINNING);
        user.setAwardDay(awardDay);
        user.setUpdateTime(System.currentTimeMillis() / 1000);

        int success = activityUserMapper.updateByExampleSelective(user, example);
        return (success == 1) ? user : null;
    }
}
